package com.sws.hmclient.controller;


import org.apache.http.util.TextUtils;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;


public class TimeUtil {
    private static TimeUtil timeUtil;
    static SimpleDateFormat formatter = null;

    public static TimeUtil getInstance(EnumChioce chioce) {
        switch (chioce) {
            case SMIPLE:
                formatter = new SimpleDateFormat("yyyy-MM-dd");
                break;
            case DETAI:
                formatter = new SimpleDateFormat("yyyyMMddHHmmss");
                break;
            case DETAIL_FORMAT:
                formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                break;
            case EIGHT:
                formatter = new SimpleDateFormat("yyyyMMdd");
                break;
        }
        if (timeUtil == null) {
            timeUtil = new TimeUtil();
        }
        return timeUtil;
    }

    public static String formatTime(String time) {
        if (!TextUtils.isEmpty(time)) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
            try {
                Date date = sdf.parse(time);
                SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
                return formatter.format(date);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        return "/";
    }

    public String getNowTime() {// HH:mm:ss
        Date curDate = new Date(System.currentTimeMillis());//获取当前时间
        String str = formatter.format(curDate);
        return str;
    }

    public static String getYestarday() {
        Date dNow = new Date();   //当前时间
        Calendar calendar = Calendar.getInstance(); //得到日历
        calendar.setTime(dNow);//把当前时间赋给日历
        calendar.add(Calendar.DATE, -1);
        Date dBefore = calendar.getTime();
        String yest = formatter.format(dBefore);
        return yest;
    }

    /**
     * 获取过去第几天的日期
     *
     * @param past
     * @return
     */
    public String getPastDate(int past) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - past);
        Date today = calendar.getTime();
        return formatter.format(today);


    }

    public static String getBeginDayofMonth(String time) {
        if (!TextUtils.isEmpty(time)) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            try {
                Date date = sdf.parse(time);
                Calendar startDate = Calendar.getInstance();
                startDate.setTime(date);
                startDate.set(Calendar.DAY_OF_MONTH, 1);
                Date firstDate = startDate.getTime();

                return sdf.format(firstDate);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        return "";

    }

    public static String getLastYear(String time) {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        Calendar startDate = Calendar.getInstance();
        startDate.setTime(new Date());
        startDate.add(Calendar.YEAR, -1);
        Date firstDate = startDate.getTime();

        return sdf.format(firstDate);


    }

    public static String getToday() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date curDate = new Date(System.currentTimeMillis());//获取当前时间
        String str = sdf.format(curDate);
        return str;
    }

    public static String getToday1() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        Date curDate = new Date(System.currentTimeMillis());//获取当前时间
        String str = sdf.format(curDate);
        return str;
    }


    /**
     * 判断两个日期大小
     *
     * @param start
     * @param end
     * @return
     */
    public static boolean judgeDay(String start, String end) {
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");//小写的mm表示的是分钟
        Date date1 = null;
        Date date2 = null;
        Calendar now1 = Calendar.getInstance();
        Calendar now2 = Calendar.getInstance();

        try {
            date1 = sdf1.parse(start);
            now1.setTime(date1);
            date2 = sdf1.parse(end);
            now2.setTime(date2);
        } catch (ParseException e) {
            e.printStackTrace();
        }


        int year1 = now1.get(Calendar.YEAR);
        int month1 = now1.get(Calendar.MONTH) + 1;
        int day1 = now1.get(Calendar.DAY_OF_MONTH);
        int year2 = now2.get(Calendar.YEAR);
        int month2 = now2.get(Calendar.MONTH) + 1;
        int day2 = now2.get(Calendar.DAY_OF_MONTH);


        if (year1 > year2) {
            return false;
        } else if (year1 == year2) {
            if (month1 > month2) {
                return false;
            } else if (month1 == month2) {
                return day1 <= day2;

            } else {
                return true;
            }
        } else {
            return year1 < year2;
        }

    }
}
